|
1
|
|
|
|
|
2
|
|
|
import window from 'window'; |
|
3
|
|
|
import {bMobileDevice, bSafari} from 'Common/Globals'; |
|
4
|
|
|
import * as Links from 'Common/Links'; |
|
5
|
|
|
import * as Events from 'Common/Events'; |
|
6
|
|
|
import {trim} from 'Common/Utils'; |
|
7
|
|
|
|
|
8
|
|
|
class Audio |
|
9
|
|
|
{ |
|
10
|
|
|
notificator = null; |
|
11
|
|
|
player = null; |
|
12
|
|
|
|
|
13
|
|
|
supported = false; |
|
14
|
|
|
supportedMp3 = false; |
|
15
|
|
|
supportedOgg = false; |
|
16
|
|
|
supportedWav = false; |
|
17
|
|
|
supportedNotification = false; |
|
18
|
|
|
|
|
19
|
|
|
constructor() { |
|
20
|
|
|
this.player = this.createNewObject(); |
|
21
|
|
|
|
|
22
|
|
|
this.supported = !bMobileDevice && !bSafari && !!this.player && !!this.player.play; |
|
23
|
|
|
if (this.supported && this.player && this.player.canPlayType) |
|
24
|
|
|
{ |
|
25
|
|
|
this.supportedMp3 = '' !== this.player.canPlayType('audio/mpeg;').replace(/no/, ''); |
|
26
|
|
|
this.supportedWav = '' !== this.player.canPlayType('audio/wav; codecs="1"').replace(/no/, ''); |
|
27
|
|
|
this.supportedOgg = '' !== this.player.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, ''); |
|
28
|
|
|
this.supportedNotification = this.supported && this.supportedMp3; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (!this.player || (!this.supportedMp3 && !this.supportedOgg && !this.supportedWav)) |
|
32
|
|
|
{ |
|
33
|
|
|
this.supported = false; |
|
34
|
|
|
this.supportedMp3 = false; |
|
35
|
|
|
this.supportedOgg = false; |
|
36
|
|
|
this.supportedWav = false; |
|
37
|
|
|
this.supportedNotification = false; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (this.supported && this.player) |
|
41
|
|
|
{ |
|
42
|
|
|
const stopFn = () => this.stop(); |
|
43
|
|
|
|
|
44
|
|
|
this.player.addEventListener('ended', stopFn); |
|
45
|
|
|
this.player.addEventListener('error', stopFn); |
|
46
|
|
|
|
|
47
|
|
|
Events.sub('audio.api.stop', stopFn); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
createNewObject() { |
|
52
|
|
|
const player = window.Audio ? new window.Audio() : null; |
|
53
|
|
|
if (player && player.canPlayType && player.pause && player.play) |
|
54
|
|
|
{ |
|
55
|
|
|
player.preload = 'none'; |
|
56
|
|
|
player.loop = false; |
|
57
|
|
|
player.autoplay = false; |
|
58
|
|
|
player.muted = false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return player; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
paused() { |
|
65
|
|
|
return this.supported ? !!this.player.paused : true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
stop() { |
|
69
|
|
|
if (this.supported && this.player.pause) |
|
70
|
|
|
{ |
|
71
|
|
|
this.player.pause(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
Events.pub('audio.stop'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
pause() { |
|
78
|
|
|
this.stop(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
clearName(name = '', ext = '') { |
|
82
|
|
|
|
|
83
|
|
|
name = trim(name); |
|
84
|
|
|
if (ext && '.' + ext === name.toLowerCase().substr((ext.length + 1) * -1)) |
|
85
|
|
|
{ |
|
86
|
|
|
name = trim(name.substr(0, name.length - 4)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return '' === name ? 'audio' : name; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
playMp3(url, name) { |
|
93
|
|
|
if (this.supported && this.supportedMp3) |
|
94
|
|
|
{ |
|
95
|
|
|
this.player.src = url; |
|
96
|
|
|
this.player.play(); |
|
97
|
|
|
|
|
98
|
|
|
Events.pub('audio.start', [this.clearName(name, 'mp3'), 'mp3']); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
playOgg(url, name) { |
|
103
|
|
|
if (this.supported && this.supportedOgg) |
|
104
|
|
|
{ |
|
105
|
|
|
this.player.src = url; |
|
106
|
|
|
this.player.play(); |
|
107
|
|
|
|
|
108
|
|
|
name = this.clearName(name, 'oga'); |
|
109
|
|
|
name = this.clearName(name, 'ogg'); |
|
110
|
|
|
|
|
111
|
|
|
Events.pub('audio.start', [name, 'ogg']); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
playWav(url, name) { |
|
116
|
|
|
if (this.supported && this.supportedWav) |
|
117
|
|
|
{ |
|
118
|
|
|
this.player.src = url; |
|
119
|
|
|
this.player.play(); |
|
120
|
|
|
|
|
121
|
|
|
Events.pub('audio.start', [this.clearName(name, 'wav'), 'wav']); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
playNotification() { |
|
126
|
|
|
if (this.supported && this.supportedMp3) |
|
127
|
|
|
{ |
|
128
|
|
|
if (!this.notificator) |
|
129
|
|
|
{ |
|
130
|
|
|
this.notificator = this.createNewObject(); |
|
131
|
|
|
this.notificator.src = Links.sound('new-mail.mp3'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if (this.notificator && this.notificator.play) |
|
135
|
|
|
{ |
|
136
|
|
|
this.notificator.play(); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
export default new Audio(); |
|
143
|
|
|
|